package com.sdp.refund.controller;


import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.sdp.core.bizc.db.Query;
import com.sdp.core.bizc.exception.ApplicationException;
import com.sdp.core.bizc.vo.DataCenter;
import com.sdp.core.bizc.vo.Result;
import com.sdp.refund.db.refund_batch.entity.RefundBatch;
import com.sdp.refund.db.refund_batch.service.IRefundBatchService;
import com.sdp.refund.db.refund_batch_detail.entity.RefundBatchDetail;
import com.sdp.refund.db.refund_batch_detail.service.IRefundBatchDetailService;
import com.sdp.refund.db.refund_user.entity.RefundUser;
import com.sdp.refund.db.refund_user.service.IRefundUserService;
import com.sdp.refund.utils.CurrentLoginEmp;
import com.sdp.refund.utils.RefundConstants;
import com.sdp.right.excel.importExcel.ImportExcelCallbackService;
import com.sdp.right.utils.RightConstants;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author zsp
 * @since 2024-08-31
 */
@RestController
@RequestMapping("/RefundBatchDetailController")
public class RefundBatchDetailController implements ImportExcelCallbackService{

	@Autowired
	private Query query;
	
	@Autowired
	private IRefundBatchDetailService iRefundBatchDetailService;
	
	@Autowired
	private IRefundBatchService iRefundBatchService;
	
	@Autowired
	private CurrentLoginEmp currentLoginEmp;
	
	@Autowired
	private IRefundUserService iRefundUserService;

	
	@PostMapping(value = "/page")
	public Result<?> page(@RequestBody DataCenter dataCenter) {
		return Result.ok(query.page(iRefundBatchDetailService, RefundBatchDetail.class, dataCenter));
	}


	@Override
	public void execute(HttpServletRequest request, HttpServletResponse response) {
		String imoprtParameter = request.getParameter("imoprtParameter");
		if(StringUtils.isEmpty(imoprtParameter)) {
			throw new ApplicationException("批次信息不存在");
		}
		String batchName = JSON.parseObject(imoprtParameter).getString("batchName");
		if(StringUtils.isEmpty(batchName)) {
			throw new ApplicationException("批次信息不存在");
		}
		
		//更新批次主表信息已完成
		LambdaUpdateWrapper<RefundBatch> batchUpdate = new LambdaUpdateWrapper<>();
		batchUpdate.set(RefundBatch::getIsComplete, RightConstants.是.getCode());
		batchUpdate.set(RefundBatch::getOpNo, currentLoginEmp.getCurrentLoginInfo().getString("empNo"));
		batchUpdate.set(RefundBatch::getOpName, currentLoginEmp.getCurrentLoginInfo().getString("empName"));
		batchUpdate.eq(RefundBatch::getBatchName, batchName);
		iRefundBatchService.update(batchUpdate);
		
		
		
		LambdaQueryWrapper<RefundBatchDetail> queryWrapper = new LambdaQueryWrapper<>();
		queryWrapper.eq(RefundBatchDetail::getBatchName, batchName);
		List<RefundBatchDetail> list = iRefundBatchDetailService.list(queryWrapper);
		if(list!=null && list.size()>0) {
			List<String> userIds = new ArrayList<>();
			for(RefundBatchDetail d :list) {
				userIds.add(d.getUserId());
			}
			
			//根据用户信息内码更新退款状态为已退款
			LambdaUpdateWrapper<RefundUser> userUpdate = new LambdaUpdateWrapper<>();
			userUpdate.set(RefundUser::getRefundState, RefundConstants.退费状态_已退费.getCode());
			userUpdate.set(RefundUser::getRefundTime, LocalDateTime.now());
			userUpdate.in(RefundUser::getKeyId, userIds);
			iRefundUserService.update(userUpdate);
			
			
		}
		
		
		
	}
	
	
	
}

